package com.yueqian.xml.mycode;

import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

public class MySaxParser {
    public static void main(String[] args) throws FileNotFoundException {
        //定义解析文件
        File xmlFile = new File("account.xml");
        if (!xmlFile.exists()) {
            throw new FileNotFoundException();
        }

        //定义解析器
        SAXParser saxParse = null;
        try {
            saxParse = SAXParserFactory.newInstance().newSAXParser();
            //定义xml事件处理器
            SAXHandler myHandler = new SAXHandler();
            //利用事件处理器myHandler来解析xmlFile,
            saxParse.parse(xmlFile, myHandler);
            //获取结果
            List<Account> list = myHandler.getAccList();
            System.out.println(list);
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


}
